package com.cyys.common.utils;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Title: CommonUtils
 * @ProjectName cyys-parent
 * @Description: 通用工具类
 * @Author lishengquan
 * @Date 2019/6/511:34
 * @Version 1.0
 */
public class CommonUtils {

    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    private static final String  MOBILE = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     * 中国移动：China Mobile
     * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     */
    private static final String CM = "^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     * 中国联通：China Unicom
     * 130,131,132,152,155,156,185,186
     */
    private static final String CU = "^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     * 中国电信：China Telecom
     * 133,1349,153,180,189
     */
    private static final String CT = "^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
     * 大陆地区固话及小灵通
     * 区号：010,020,021,022,023,024,025,027,028,029
     * 号码：七位或八位
     */
    private static final String PHS = "^0(10|2[0-5789]|\\d{3})\\d{7,8}$";


/*    private final static String[] baseString={"0","1","2","3", "4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j",
            "k","l","m","n","o","p","q","r","s","t", "u","v","w","x","y", "z","A","B","C","D",
            "E","F","G","H","I", "J","K","L","M","N","O","P","Q","R","S","T","U","V","W",
            "X","Y","Z",",",".","@","!","#","%","$","-"};*/

    private final static String[] baseString={"0","1","2","3", "4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j",
            "k","l","m","n","o","p","q","r","s","t", "u","v","w","x","y", "z","A","B","C","D",
            "E","F","G","H","I", "J","K","L","M","N","O","P","Q","R","S","T","U","V","W",
            "X","Y","Z"};

    /**
     * 依次校验上诉电话正则 不匹配返回false 否则返回true
     * @description: 电话格式校验
     * @param
     * @return ${return_type}
     * @throws
     * @author lishengquan
     * @date 2019/6/5 11:36
     * @version 1.0
     */
    public static boolean checkPhone(String phone) {
        Pattern p;
        Matcher m;
        boolean flag;
        //匹配 一般电话规则
        p = Pattern.compile(MOBILE);
        m = p.matcher(phone);
        flag = m.matches();
        //是否匹配胡定电话
        if(!flag){
            p = Pattern.compile(PHS);
            m = p.matcher(phone);
            flag = m.matches();
        }
        return flag;
    }


    /**
     * 富文本内容转义 反转
     * @description:   ajax会将html内容转义 <>标签转义为& lt; & gt;
     * @param
     * @return ${return_type}
     * @throws
     * @author lishengquan
     * @date 2019/6/7 10:54
     * @version 1.0
     */
    public static String htmlContentToTag(String transferredHtml) {
        String content = transferredHtml.replaceAll("& lt;", "<").replaceAll("& gt;", ">");
        return content;
    }


    /**
     * @Author: lsq
     * @Description: 生成指定位数的随机密码
     * @Date: 2021/9/18
     * @Param pdLen: 密码长度
     * @return: String:密码
     **/
    public static String getRandomPassword(int pdLen) {
        Random random = new Random();
        int length = baseString.length;
        StringBuffer randomString = new StringBuffer();
        for(int i=0;i<length;i++){
            randomString.append(baseString[random.nextInt(length)]);
        }
        random = new Random(System.currentTimeMillis());
        StringBuffer resultStr = new StringBuffer();
        for (int i = 0; i < pdLen; i++) {
            resultStr.append(randomString.toString().charAt(random.nextInt(randomString.length()-1)));
        }
        return resultStr.toString();
    }
}
